package a66weding.com.jiehuntong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;


import org.xutils.x;

import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.beans.FriendsListBean;
import a66weding.com.jiehuntong.beans.PersonBean;
import a66weding.com.jiehuntong.common.utils.ImageOptionsUtil;
import a66weding.com.jiehuntong.inner.OnItemClickLitener;
import a66weding.com.jiehuntong.widgets.CircularImage;

/**
 * Created by admin on 2016/7/25.
 */
public class AddFriends_2_Adpter extends BaseRecyclerAdapter<AddFriends_2_Adpter.ViewHolder> {


    //如果有点击事件请设置一个监听;
    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    private Context context;
    private List<PersonBean.DataBean> data;
    public AddFriends_2_Adpter(Context context, List<PersonBean.DataBean> data){
        this.context = context;
        this.data = data;
    }
    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View v = LayoutInflater.from(context).inflate(
                R.layout.item_add_friends, parent, false);
        ViewHolder holder = new ViewHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position, boolean isItem) {
        String name_tv = data.get(position).getNvc_Name();
        if( name_tv==null||"".equals(name_tv)){
            holder.name.setText("--");
        }else{
            holder.name.setText(name_tv);
        }
        holder.phone.setText(data.get(position).getNvc_MobilePhone());
        if(data.get(position).getNvc_TitlePhoto()!=null){
            x.image().bind(holder.head,""+data.get(position).getNvc_TitlePhoto(), ImageOptionsUtil.getOptions292_700());
        }
       if(mOnItemClickLitener!=null){
           holder.itemView.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   int pos = holder.getLayoutPosition();
                   mOnItemClickLitener.onItemClick(holder.itemView,pos);
               }
           });
       }
    }

    public void append(List<PersonBean.DataBean> data) {
        if (this.data != null) {
            this.data.addAll(data);
        } else {
            this.data = data;
        }
        notifyDataSetChanged();
    }

    @Override
    public int getAdapterItemCount() {
        if(data==null){
            return 0;
        }else{
            return data.size();
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        //头像
        private CircularImage head;
        //昵称
        private TextView name;
        //账号
        private TextView phone;

        public ViewHolder(View itemView) {
            super(itemView);
            head = (CircularImage) itemView.findViewById(R.id.item_addfriends_2_icon);
            name = (TextView) itemView.findViewById(R.id.item_addfriends_2_name);
            phone = (TextView) itemView.findViewById(R.id.item_addfriends_2_phone_text);

        }
    }
    public void clear() {
        if (data != null) {
            this.data.clear();
        }
    }
}
